﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    class JLTRWB18Report : BaseReport
    {
        #region ----------------PROPERTIES----------------------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        public DataSet setUI = new DataSet();
        public string ARTICLE ,COLORNO,COM = "";
        string DocType;
        string CellName = "";
        string CellValue = "";
        string strCompany;
        string strExcelFile;
        Dictionary<string, int> qpplan = new Dictionary<string, int>();
        
        #endregion
        public JLTRWB18Report()
            : base()
        {
        }
        public JLTRWB18Report(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            if (dicParamater.ContainsKey("ROOTDRIVE"))
            {
                Root = dicParamater["ROOTDRIVE"].ToString();
            }
            else
            {
                Root = "C:\\IntexReport";
            }
                      						

        } 
        #region------------------------------Function------------------------
        /* WBBEINDAT	SHIFT	WBFFSCHL	WBBWARSCH	WBBWMPOS	ARTICLE	STRU	WBBSTKNR	
                STARTMTR	ENDMTR	MTR	FMTR SMTR	TMTR	OMTR	QRATE	QTY	DDIS	WBBWBMLFDNR*/
        public void FillInExcelData(DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["RWB18"];                
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("WBBEINDAT", typeof(System.String));
                dtOutput.Columns.Add("SHIFT", typeof(System.String));
                dtOutput.Columns.Add("SCHL", typeof(System.String));
                dtOutput.Columns.Add("WBFFSCHL", typeof(System.String));
                dtOutput.Columns.Add("WBBWARSCH", typeof(System.String));
                dtOutput.Columns.Add("WBBWMPOS", typeof(System.String));
                dtOutput.Columns.Add("ARTICLE", typeof(System.String));
                dtOutput.Columns.Add("STRU", typeof(System.String));
                dtOutput.Columns.Add("WBBSTKNR", typeof(System.String));
                dtOutput.Columns.Add("PCEMTR", typeof(System.String));
                dtOutput.Columns.Add("STARTMTR", typeof(System.String));
                dtOutput.Columns.Add("ENDMTR", typeof(System.String));
                dtOutput.Columns.Add("MTR", typeof(System.String));
                dtOutput.Columns.Add("FMTR", typeof(System.String));
                dtOutput.Columns.Add("SMTR", typeof(System.String));
                dtOutput.Columns.Add("TMTR", typeof(System.String));
                dtOutput.Columns.Add("OMTR", typeof(System.String));
                dtOutput.Columns.Add("QRATE", typeof(System.String));
                dtOutput.Columns.Add("QTY", typeof(System.String));
                dtOutput.Columns.Add("DDIS", typeof(System.String));
                dtOutput.Columns.Add("WBBWBMLFDNR", typeof(System.String));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);

                exAgent.DataTableToExcel(dtOutput, 2, 1);

                int count = dtOutput.Rows.Count;
                exAgent.SetCellValue(1, 1, 1);
                exAgent.BatchConvert2Number(2, 3, count + 1, 3);
                exAgent.BatchConvert2Number(2, 6, count + 1, 6);
                exAgent.BatchConvert2Number(2, 10, count + 1, 17);
                exAgent.BatchConvert2Number(2, 19, count + 1, 21);
                exAgent.SetCellValue(1, 1, "检验日期");
                						
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void FillInExcel(DataSet setSoruce, int xStart = 2, int yStart = 1)
        {
            try
            {
                
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["RWB18"];
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("WBBEINDAT", typeof(System.String));
                dtOutput.Columns.Add("SHIFT", typeof(System.String));
                dtOutput.Columns.Add("SCHL", typeof(System.String));
                dtOutput.Columns.Add("WBFFSCHL", typeof(System.String));
                dtOutput.Columns.Add("WBBWARSCH", typeof(System.String));
                dtOutput.Columns.Add("WBBWMPOS", typeof(System.String));
                dtOutput.Columns.Add("ARTICLE", typeof(System.String));
                dtOutput.Columns.Add("STRU", typeof(System.String));
                dtOutput.Columns.Add("WBBSTKNR", typeof(System.String));
                dtOutput.Columns.Add("PCEMTR", typeof(System.String));
                dtOutput.Columns.Add("STARTMTR", typeof(System.String));
                dtOutput.Columns.Add("ENDMTR", typeof(System.String));
                dtOutput.Columns.Add("MTR", typeof(System.String));
                dtOutput.Columns.Add("FMTR", typeof(System.String));
                dtOutput.Columns.Add("SMTR", typeof(System.String));
                dtOutput.Columns.Add("TMTR", typeof(System.String));
                dtOutput.Columns.Add("OMTR", typeof(System.String));
                dtOutput.Columns.Add("QRATE", typeof(System.String));
                dtOutput.Columns.Add("QTY", typeof(System.String));
                dtOutput.Columns.Add("DDIS", typeof(System.String));
                dtOutput.Columns.Add("WBBWBMLFDNR", typeof(System.String));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);

                exAgent.DataTableToExcel(dtOutput, 2, 1);
                int i = 2;
                string ROLL = "";
                foreach (DataRow dw in dtSoruce.Rows)
                {
                    string WBBSTKNR = dw["WBBSTKNR"].ToString().Trim();
                    if (ROLL == WBBSTKNR)
                    {/*
                        string SHIFT = dw["SHIFT"].ToString().Trim();
                        exAgent.SetCellValue(i, 2, SHIFT);
                        string SCHL = dw["SCHL"].ToString().Trim();
                        exAgent.SetCellValue(i, 3, SCHL);
                        string WBFFSCHL = dw["WBFFSCHL"].ToString().Trim();
                        exAgent.SetCellValue(i, 4, WBFFSCHL);                        
                        string STARTMTR = dw["STARTMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 11, STARTMTR);
                        string ENDMTR = dw["ENDMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 12, ENDMTR);
                        string MTR = dw["MTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 13, MTR);
                        string FMTR = dw["FMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 14, FMTR);
                        string SMTR = dw["SMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 15, SMTR);
                        string TMTR = dw["TMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 16, TMTR);
                        string OMTR = dw["OMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 17, OMTR);
                        string QRATE = dw["QRATE"].ToString().Trim();
                        exAgent.SetCellValue(i, 18, QRATE);
                        string QTY = dw["QTY"].ToString().Trim();
                        exAgent.SetCellValue(i, 19, QTY);
                        */
                        exAgent.SetCellValue(i, 1, "");
                        exAgent.SetCellValue(i, 5, "");
                        exAgent.SetCellValue(i, 6, "");
                        exAgent.SetCellValue(i, 7, "");
                        exAgent.SetCellValue(i, 8, "");
                        exAgent.SetCellValue(i, 9, "");
                        exAgent.SetCellValue(i, 10, "");
                        exAgent.SetCellValue(i, 20, "");
                        exAgent.SetCellValue(i, 21, "");
                    }
                    else
                    {/*
                        string WBBEINDAT = dw["WBBEINDAT"].ToString().Trim();
                        exAgent.SetCellValue(i, 1, WBBEINDAT);
                        string SHIFT = dw["SHIFT"].ToString().Trim();
                        exAgent.SetCellValue(i, 2, SHIFT);
                        string SCHL = dw["SCHL"].ToString().Trim();
                        exAgent.SetCellValue(i, 3, SCHL);
                        string WBFFSCHL = dw["WBFFSCHL"].ToString().Trim();
                        exAgent.SetCellValue(i, 4, WBFFSCHL);
                        string WBBWARSCH = dw["WBBWARSCH"].ToString().Trim();
                        exAgent.SetCellValue(i, 5, WBBWARSCH);
                        string WBBWMPOS = dw["WBBWMPOS"].ToString().Trim();
                        exAgent.SetCellValue(i, 6, WBBWMPOS);
                        string ARTICLE = dw["ARTICLE"].ToString().Trim();
                        exAgent.SetCellValue(i, 7, ARTICLE);
                        string STRU = dw["STRU"].ToString().Trim();
                        exAgent.SetCellValue(i, 8, STRU);
                        exAgent.SetCellValue(i, 9, WBBSTKNR);
                        string PCEMTR = dw["PCEMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 10, PCEMTR);
                        string STARTMTR = dw["STARTMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 11, STARTMTR);
                        string ENDMTR = dw["ENDMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 12, ENDMTR);
                        string MTR = dw["MTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 13, MTR);
                        string FMTR = dw["FMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 14, FMTR);
                        string SMTR = dw["SMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 15, SMTR);
                        string TMTR = dw["TMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 16, TMTR);
                        string OMTR = dw["OMTR"].ToString().Trim();
                        exAgent.SetCellValue(i, 17, OMTR);
                        string QRATE = dw["QRATE"].ToString().Trim();
                        exAgent.SetCellValue(i, 18, QRATE);
                        string QTY = dw["QTY"].ToString().Trim();
                        exAgent.SetCellValue(i, 19, QTY);
                        string DDIS = dw["DDIS"].ToString().Trim();
                        exAgent.SetCellValue(i, 20, DDIS);
                        string WBBWBMLFDNR = dw["WBBWBMLFDNR"].ToString().Trim();
                        exAgent.SetCellValue(i, 21, WBBWBMLFDNR);
                        */
                        ROLL = WBBSTKNR;
                    }

                    i = i + 1;
                }
                int count = dtSoruce.Rows.Count;
                exAgent.SetCellValue(1, 1, 1);
                exAgent.BatchConvert2Number(2, 10, count + 1, 17);
                exAgent.BatchConvert2Number(2, 19, count + 1, 20);
                exAgent.SetCellValue(1, 1, "检验日期");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }        
        public override void WriteExcelReport(string fileType)
        {
            bool IsFTP = false;
            try
            {
                strCompany = "JLT";
                strExcelFile = @"JLT\JLTReport.xls";
                Standard = "RWB18";
                TemplatePath = Path.Combine(PathHelper.ApplicationPath, @"Report\JLT\JLTReport.xls");
                destfolder = SetFolder(Root, "坯布检验");
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                string newfilename = @"档车工产量" + filedate;
                
                exAgent.CheckWorkBook(newfilename);
                ServerShareFile("", year, month, newfilename, fileType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ServerShareFile(string KRS, string year, string month, string ordernumer, string fileType)
        {
            string destfile, tempfile = "";
            string fileabsname = ordernumer;
            try
            {
                filename = destfolder + "\\" + fileabsname + ".xls"; ;
                destfile = filename;
                tempfile = CreateFile(ordernumer);
                if (File.Exists(destfile))
                {
                    File.Delete(destfile);
                }
                File.Move(tempfile, destfile);

                if (fileType == "OpenExcel")
                {
                    System.Diagnostics.Process.Start("EXCEL.EXE", filename);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                if (File.Exists(tempfile))
                {
                    File.Delete(tempfile);
                }
            }
        }
        private string CreateFile(string orderno)
        {
            string filename = "";
            try
            {
                string pwd = "";
                string sheetName = orderno;
                exAgent.Open(TemplatePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                exAgent.CreateNewSheet(orderno + ".xls", Standard, sheetName, pwd);
                filename = exAgent.FileName;
                exAgent.SetCurrentSheet(1);
                if (COM == "N")
                {
                    FillInExcelData(dsSource, 3, 1);
                }
                else
                {
                    FillInExcel(dsSource, 3, 1);
                }
                exAgent.Save();
                exAgent.Close();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return filename;
        }
        #endregion
    }
}